<?php

namespace app\api\controller;

use app\facade\Redis;
use app\models\{Shoplist, Cate, Banner, Goods, GoodsSpec, Region, Helps, Collect, Serser};
use think\Facade\Config as Configs;
use EasyWeChat\Factory;
use think\Validate;
use think\Db;
use think\facade\Log;

class Other extends Base
{
    public function other()
    {
        $list['key'] = '2AEBZ-YK23W-XISR6-ROOGV-VOLI7-46FFT';
        $this->result($list, 0, '获取成功');
    }

    public function helps()
    {
        if ($this->request->isPost()) {
            $id  = $this->request->post('id/d', '0');
            $helpmodel = new Helps();
            $list = $helpmodel->where('id', $id)->field('id,title,content')->find();
            $this->result($list, 0, '获取成功');
        } else {
            $helpmodel = new Helps();
            $type  = $this->request->get('type/d', 0);
            $list = $helpmodel->field('id,title,content');
            if (!empty($type)) {
                $list = $list->where('type', $type);
            }
            $list = $list->select();
            $this->result($list, 0, '获取成功');
        }
    }

    public function getshopid()
    {
        $user = $this->request->user;
        $model = new Shoplist;
        $result = [];
        if ($user['shopid'] > 0) {
            $result = $model->where('id', $user['shopid'])->find();
        }
        //获取附近的商家
        if (!$result) {
            $lon  = $this->request->get('lon/f', '0');
            $lat  = $this->request->get('lat/f', '0');
            $filed = 'ROUND(6378.138*2*ASIN(SQRT(POW(SIN((' . $lat . '*PI()/180-lat*PI()/180)/2),2)+COS(' . $lat . '*PI()/180)*COS(lat*PI()/180)*POW(SIN((' . $lon . '*PI()/180-lon*PI()/180)/2),2)))*100)/100 AS dist';
            $result = $model->field('id,title,' . $filed)
                ->order('dist asc')
                ->find();
        }
        $this->result($result, 0, '获取成功');
    }

    public function cate()
    {
        $model = new Cate();
        $shopid  = $this->request->get('shopid/d', 0);
        $ishot  = $this->request->get('ishot/d', 0);
        $result = $model->order('sorts desc')->where('ishow', 1)->where('shopid', $shopid)->field('id,name,remark');
        if ($ishot) {
            $result = $result->where('ishot', $ishot);
        }
        $result = $result->select();
        $this->result($result, 0, '获取成功');
    }

    public function banner()
    {
        $model = new Banner();
        $shopid  = $this->request->get('shopid/d', 0);
        $result = $model->order('sorts desc')->where('shopid', $shopid)->where('ishow', 1)->field('type,banner,getid,types');
        $result = $result->select();
        $this->result($result, 0, '获取成功');
    }

    public function goodlist()
    {
        $data  = $this->request->get();
        if (empty($data['shopid'])) {
            $this->result([], 1, '商户ID必须上传');
        }
        $model = new Goods();
        $model_son = new GoodsSpec();
        $result = $model->where(['isSale' => 1, 'isdel' => 1]);
        if (!empty($data['cate'])) {
            $result = $result->order('sale desc')->where('cate', $data['cate']);
        }
        if (!empty($data['isbest'])) {
            $result = $result->order('sale desc')->where('isBest', $data['isbest']);
        }
        if (!empty($data['rund'])) {
            $result = $result->orderRaw('rand()');
        }
        if (!empty($data['title'])) {
            (new Serser())->save([
                'shopid' => $data['shopid'],
                'title' => $data['title']
            ]);
            $result = $result->where('title|Keywords', 'like', '%' . $data['title'] . '%');
        }
        $result = $result->where('shopid', $data['shopid'])->field('id,title,images,descs,isSale,isBest,isHot,isNew,sale,cate')->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($result as $results) {
            $results['images'] = json_decode($results['images'], true);
            $results['spec'] = $model_son->order('is_default desc')->where('isdel', 1)->where('goodid', $results['id'])->field('id,name,price,oprice,goodsstock')->select();
        }
        $this->result($result, 0, '获取成功');
    }

    public function gooddetail()
    {
        $id  = $this->request->get('id/d', 0);
        $model = new Goods();
        $model_son = new GoodsSpec();
        $collectmodel = new Collect();
        $result = $model->where(['isSale' => 1, 'isdel' => 1, 'id' => $id])->field('id,goodsDesc,title,images,descs,isSale,isBest,isHot,isNew,sale,parpam')->find();
        if ($result) {
            $result['images'] = json_decode($result['images'], true);
            $result['parpam'] = json_decode($result['parpam'], true);
            $result['collect'] = 0;
            if (!empty($this->request->user_id)) {
                $result['collect'] = !empty($collectmodel->where(['user_id' => $this->request->user_id, 'goodsid' => $id])->find()) ? 1 : 0;
            }
            $result['spec'] = $model_son->order('is_default desc')->where('isdel', 1)->where('goodid', $result['id'])->field('id,name,price,oprice,goodsstock')->select();
        }
        $this->result($result, 0, '获取成功');
    }


    public function getaddress()
    {
        $addressjsonredis = 'addressjsonredis';
        $haveaddress = Redis::get($addressjsonredis);
        if ($haveaddress) {
            $result = unserialize($haveaddress);
        } else {
            $region = (new Region())->field('name as label,area_code as value,id,parent_id,level')->where(['level' => [1, 2, 3]])->select();
            $resultson = [];
            $results = [];
            $result = [];
            foreach ($region as $regions) {
                if ($regions['level'] == 3) {
                    $resultson[$regions['parent_id']][] = [
                        'label' => $regions['label'],
                        'value' => $regions['value']
                    ];
                }
            }
            foreach ($region as $regions) {
                if ($regions['level'] == 2) {
                    $results[$regions['parent_id']][] = [
                        'label' => $regions['label'],
                        'value' => $regions['value'],
                        'children' => $resultson[$regions['id']] ?? []
                    ];
                }
            }
            foreach ($region as $regions) {
                if ($regions['level'] == 1) {
                    $result[] = [
                        'label' => $regions['label'],
                        'value' => $regions['value'],
                        'children' => $results[$regions['id']] ?? []
                    ];
                }
            }
            Redis::setex($addressjsonredis, 60 * 60 * 3, serialize($result));
        }
        return json($result);
    }

    // public function ceshi()
    // {
    //     $url = 'https://sapi.ztpay.org/api/v2';
    //     // $parm =     [
    //     //     "appid" => "ztpayewikiq4rwvitw",
    //     //     "method" => "transfer",
    //     //     "name" => "FIL",
    //     //     "from" => "f176lw23w73na7wbn6ykq4sx6kczecsvr7bew7xja",
    //     //     "to" => "f14mqd4xybghk2er273qor7wufwhworubyjycfpki",
    //     //     "amount" => "0.0001"
    //     // ];
    //     // $sign = 'amount=' . $parm['amount'] . '&appid=' . $parm['appid'] . '&from=' . $parm['from'] . '&method=' . $parm['method'] . '&name=' . $parm['name'] . '&to=' . $parm['to'] . '&key=vyaxBxrLUjx4qVyX34URsXhjWgSuElTL';

    //     $parm = [
    //         "appid" => "ztpayewikiq4rwvitw",
    //         "method" => "transaction_detail",
    //         "name" =>  "FIL",
    //         "hash" =>  "bafy2bzacebd5nndtdpcc3rt7b5zhqwczbuvlcqk2l52br6k3fynuy3fykobmw",
    //         "tr_native" =>  1,
    //     ];
    //     $sign = 'appid=' . $parm['appid'] . '&hash=' . $parm['hash'] . '&method=' . $parm['method'] . '&name=' . $parm['name'] . '&tr_native=' . $parm['tr_native'] . '&key=vyaxBxrLUjx4qVyX34URsXhjWgSuElTL';

    //     $parm['sign'] = strtoupper(md5($sign));
    //     $result = api_curl($url, $parm, 1);
    //     $result = json_decode($result, true);
    //     return json($result);
    // }

    // public function ceshi2()
    // {
    //     $data  = $this->request->post();
    //     Log::write($data, 'ceshi');
    //     if (!empty($data['data']['state']) && $data['data']['state'] == 1) {
    //         return 'SUCCESS';
    //     }
    //     return false;
    // }
}
